// API - 访问用户控制

import { http } from "@/utils/http";
import { errorReject } from "@/utils/truncate";
import { message } from "@/utils/message";
import { type TokenInfo, type DataInfo, formatToken } from "@/utils/auth";

export interface UserTokenResult extends HttpResponse {
  data: TokenInfo;
}

export interface UserResult extends HttpResponse {
  data: DataInfo;
}

export interface MessageResult extends HttpResponse {
  data: {
    /** `未读消息数` */
    totalUnread: number;
    /** `消息列表` */
    list: {
      id: Id;
      title: string;
      description: string;
      isRead: boolean;
      createTime: string;
    }[];
  };
}

interface LogsResult extends HttpResponse {
  data?: {
    /** 列表数据 */
    list: {
      id: Id;
      ipAddress: string;
      ipLocation: string;
      loginSystem: string;
      loginBrowser: string;
      summary: string;
      createTime: string;
    }[];
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
}

/**
 * 用户登录
 *
 * @param data 请求参数
 * @returns
 */
export const accessUserLogin = (data?: object) => {
  return errorReject(
    http.request<UserTokenResult>("post", "/access/login", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 获取已登录用户信息
 *
 * @returns
 */
export const getAccessUserInfo = (tokenData?: TokenInfo) => {
  const headers: any = {};
  if (tokenData) {
    headers.Authorization = formatToken(tokenData.accessToken);
  }
  return errorReject(
    http.request<UserResult>("get", "/access/info", {
      headers
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 修改已登录用户信息
 *
 * @param data 请求参数
 * @returns
 */
export const updateAccessUserInfo = (data?: object) => {
  return errorReject(
    http.request<UserResult>("put", "/access/info", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 刷新Token
 *
 * @param data 请求参数
 * @returns
 */
export const refreshAccessToken = (data?: object) => {
  return errorReject(
    http.request<UserTokenResult>(
      "post",
      "/access/refresh-token",
      {
        data
      },
      { quite: true }
    ),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 修改已登录用户密码
 *
 * @param data 请求参数
 * @returns
 */
export const changeAccessUserPassword = (data?: object) => {
  return errorReject(
    http.request<UserResult>("put", "/access/password", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 获取用户系统消息
 *
 * @returns
 */
export const getAccessUserMessages = () => {
  return http.request<MessageResult>(
    "get",
    "/access/messages",
    {},
    { quite: true }
  );
};

/**
 * 获取用户日志分页列表
 *
 * @param data 请求参数
 * @returns
 */
export const getUserLogsPageList = (data?: object) => {
  return errorReject(
    http.request<LogsResult>("post", "/access/logs/page", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};
